package cn.tedu.thread;
//测试 多线程售票
public class Test4_Tickets {
    public static void main(String[] args) {
        MyTickets t1 = new MyTickets();
        t1.start();
        MyTickets t2 = new MyTickets();
        t2.start();
        MyTickets t3 = new MyTickets();
        t3.start();
        MyTickets t4 = new MyTickets();
        t4.start();
    }
}
class MyTickets extends Thread{
    //加statci的原因是：把tickets变成共享资源，在全局是唯一的一份
    static int tickets = 100;//定义变量，记录票数
    Object o = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (MyTickets.class) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    break;
                }
                System.out.println(getName() + "===" + tickets--);
            }
        }
    }
}